import {createOrder} from "./api.js";

function _createOrder(goodsId, couponId, type, token) {
	switch (type) {
		case 'wxpay':
			return createOrder(goodsId, couponId, 0, token);
		case 'alipay':
			return createOrder(goodsId, couponId, 1, token);
		case 'appleiap':
			return createOrder(goodsId, couponId, 2, token);
		default:
			return Promise.reject(new Error('无效的支付方式'))
	}
}

function _provider(type='', order, productIds=[]) {
    // #ifdef H5
        return Promise.reject(new Error('H5平台暂不支持支付功能'));
    // #endif
    // #ifndef H5
        return new Promise((resolve, reject) => {
            uni.getProvider({
                service: 'payment',
                success(res) {
                    if (type === 'appleiap') {
                        let iapChannel = res.providers.find((channel) => {
                            return channel.id === type;
                        });
                        if (iapChannel) {
							let orderInfo = {
								productid: order.bindId,
								username: order.orderId,
								quantity: 1,
								manualFinishTransaction:true,
								optimize: true
							};
                            iapChannel.requestOrder(productIds, res => {
                                resolve({
                                    provider: type,
                                    orderInfo: orderInfo,
                                });
                            }, e => {
                                reject(e);
                            });
                        } else {
                            reject(new Error('暂不支持iap支付'));
                        }
                    } else if (~res.provider.indexOf(type)) {
                        resolve({
                            provider: type,
                            orderInfo: order,
                        });
                    } else if (~res.provider.indexOf('toutiao')) {
                        resolve({
                            provider: 'toutiao',
                            orderInfo: {
                                order: order.data
                            },
                        });
                    } else {
                        reject(new Error('暂不支持此类型支付'));
                    }
                },
                fail(err) {
                    reject(err);
                }
            });
        });
    // #endif
}

function _payment(data) {
    return new Promise((resolve, reject) => {
        let options;
        // #ifdef MP
        options = {
            service: 5,
            provider: data.provider,
            orderInfo: data.orderInfo.order,
            success(res) {
                if (data.provider !== 'toutiao' || res.code === 0) {
                    resolve({
                        provider: data.provider,
                        orderInfo: data.orderInfo.order,
                        response: res,
                    });
                } else {
                    reject(new Error('cancel'));
                }
            },
            fail(err) {
                reject(err);
            }
        }
        if (data.orderInfo['jsApiParameters']) {
            let jsApiParameters = JSON.parse(data.orderInfo['jsApiParameters']);
            Object.assign(options, jsApiParameters);
        }
        // #endif
        // #ifndef MP
        options = {
            provider: data.provider,
            orderInfo: data.orderInfo,
            success(res) {
				console.log('pay success ==========>',res)
                resolve({
                    provider: data.provider,
                    orderInfo: data.orderInfo,
                    response: res,
                });
            },
            fail(err) {
                reject(err);
            }
        }
        // #endif
        uni.requestPayment(options);
    });
}

function payment(goodsId, couponId, type, productIds, token) {
    return _createOrder(goodsId, couponId, type, token)
        .then(order => _provider(type, order, productIds))
        .then(res => _payment(res))
}

export default payment;
